package com.dg.lecheng.manager.controller;

import com.dg.lecheng.api.user.model.InfoAuth;
import com.dg.lecheng.api.user.service.InfoAuthService;
import com.dg.lecheng.base.core.model.BaseResult;
import com.dg.lecheng.base.core.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/10/22 0022.
 */
@Api(value = "InfoAuthController" ,description = "资源Controller")
@Controller
@RequestMapping("/infoAuth")
public class InfoAuthController extends BaseController{

    @Autowired
    private InfoAuthService infoAuthService;

    @ApiOperation("获取资源列表")
    @RequestMapping(value = "/getList" ,method = RequestMethod.GET)
    @ResponseBody
    public Object getList(@ApiParam(name = "name" ,value = "资源名称") @RequestParam(value = "name",required = false) String name){
        List<InfoAuth> infoAuths = infoAuthService.findByName(name,getPageBounds());
        return BaseResult.success(infoAuths);
    }

    @ApiOperation("获取资源列表作为树的数据")
    @RequestMapping(value = "/getListForZtree" ,method = RequestMethod.GET)
    @ResponseBody
    public Object getListForZtree(){
        return BaseResult.success(infoAuthService.getListForZtree());
    }

    @ApiOperation("获取单个资源")
    @RequestMapping(value = "get/{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable int id){
        return BaseResult.success(infoAuthService.get(id));
    }

    /**
     * 添加资源信息
     * @param infoAuth
     * @return
     */
    @ApiOperation("添加资源")
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    @ResponseBody
    public Object add(@ApiParam(name = "infoAuth" ,value = "用户model") @RequestBody InfoAuth infoAuth){
        if(infoAuth.getId() == null){
            infoAuthService.add(infoAuth);
        }else{
            infoAuthService.update(infoAuth);
        }
        return BaseResult.success(null);
    }

    @ApiOperation("删除资源")
    @RequestMapping(value = "/delete/{id}" ,method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@PathVariable int id){
        infoAuthService.removeAuthAndRelatedInfo(id);
        return BaseResult.success(null);
    }

    @ApiOperation("根据角色Id获取角色资源")
    @RequestMapping(value = "/findRoleAuthByRoleId/{roleId}" ,method = RequestMethod.GET)
    @ResponseBody
    public Object findRoleAuthByRoleId(@ApiParam(name = "roleId" ,value = "角色ID") @PathVariable int roleId){
        return BaseResult.success(infoAuthService.findRoleAuthByRoleId(roleId));
    }

    @ApiOperation("查找父类")
    @RequestMapping(value = "/findParentElement/{type}" ,method = RequestMethod.GET)
    @ResponseBody
    public Object findParentElement(@ApiParam(name = "type" ,value = "子类型") @PathVariable int type){
        if(type == 1)
            return BaseResult.success(infoAuthService.findParentElement(1));
        return BaseResult.success(infoAuthService.findParentElement(type-1));
    }
}
